import { defineStore } from "pinia";
import {
    reqSchoolAll,
    reqSchoolList,
    reqSchoolAdd,
    reqSchoolById,
    reqSchoolUpdata,
    reqSchoolDelete
} from "@/api/school"
import type {
    schoolInfoData,
    schoolListFormData,
    schoolResponseData,
    schoolAllResponseData,
    schoolListResponseData
} from "@/api/school/type"
import { SchoolState } from "./types/type";
let useSchoolStore = defineStore("School",{
    state:():SchoolState=>{
      return {
        schools:[]
      }
    },
    actions:{
        // 总
        async getSchoolAll(){
            let result:schoolAllResponseData = await reqSchoolAll();
            if(result.status === 0){
                this.schools=(result.data as schoolInfoData[])
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 部分
        async getSchoolList(data:schoolListFormData){
            let result:schoolListResponseData =await reqSchoolList(data);
            if(result.status ===0){
                this.schools=(result.data?.data as schoolInfoData[])
            return result.data
            }else{
            return Promise.reject(new Error(result.msg));
            }
        },
        // 添加
        async addschool(data:schoolInfoData){
            let result:schoolResponseData =await reqSchoolAdd(data);
            if(result.status ==0){
            return "OK"
            }
            else{
              return Promise.reject (new Error(result.msg))
            }
            },
          // ID获取
          async getSchoolById(_id:string){
            let result:schoolResponseData =await reqSchoolById(_id)
              if(result.status ===0){
              return result.data
              }else{
              return Promise.reject(new Error(result.msg))
      
              }
          },
          // 修改
          async updateSchool(data:schoolInfoData){
            let result:schoolResponseData =await reqSchoolUpdata(data);
              if(result.status ==0){
              return "OK"
              }
              else{
              return Promise.reject(new Error(result.msg))
              }
          },
          // 删除
          async deleteSchool(schoolId:string){
            let result:schoolResponseData =await reqSchoolDelete(schoolId);
              if(result.status ==0){
              return "OK"
              }
              else{
              return Promise.reject(new Error(result.msg))
              }
          },
          getters:{
    
          }
    }
    
})
export default useSchoolStore